import 'dart:math';

import 'package:ladder_cash/app/com/plugin/common_plugin.dart';
import 'package:liuxlcommon/view/lxl_widget.dart';
import 'package:ladder_cash/app/com/service/burial_point_service.dart';
import 'package:ladder_cash/app/com/view/common_view.dart';
import 'package:ladder_cash/app/com/view/webview/my_web/my_web_page.dart';
import 'package:ladder_cash/app/key_mix.dart';
import 'package:ladder_cash/app/page/login/login_page.dart';
import 'package:ladder_cash/gen/assets.gen.dart';
import 'package:ladder_cash/generated/assets.dart' as myAssets;
import 'package:ladder_cash/generated/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:ladder_cash/app/app.dart';
import 'package:ladder_cash/app/comm_key_def.dart';
import 'package:ladder_cash/app/com/http/http_request.dart';
import 'package:ladder_cash/app/com/service/reported_data_service.dart';
import 'package:ladder_cash/app/router_manage.dart';
import 'package:liuxlcommon/util/string_utils.dart';
import 'package:webview_flutter/webview_flutter.dart';

import '../../../library/src/logger.dart';

class IndexLogic extends GetxController {
  Map<String, dynamic>? countData;

  bool dateExpand = false;

  bool isQs = true;

  int page = 1;
  bool isAgree = true;

  int tabIndex = 0;

  bool? isBanner;

  Map? indexData;

  List? banners;

  /// true  大卡位，false 小卡为
  bool isLargeCard = true;

  ///大卡位
  Map? largeCardData;

  ///小卡位
  Map? smallCardData;

  ///产品列表
  List? productList;

  /// 关于我们

  int aboutState = 0;

  changeState(int state) {
    aboutState = state;
    update();
  }

  final List<Map<String, dynamic>> antesDeList = [
    {'title': "1", 'content': S.current.adultaA},
    {'title': "2", 'content': S.current.sin},
    {'title': "3", 'content': S.current.hayUna},
  ];

  changeAgree() {
    isAgree = !isAgree;
    update();
  }

  refreshData() {
    page = 1;
    update();
    return;
  }

  Future<void> getIndexData({refresh = false}) async {
    CommonPlugin.initAdjust;
    indexData = await HttpRequest().get('/v4/index/home-page'.hx, null);
    if (indexData != null) {
      Map lingeringMap = {};
      for (dynamic data in indexData?['list'.hx] ?? []) {
        String key = data['type'.hx];
        // lingeringMap[key] = data['disinterested'];
        if (key == CommKeyDef.banner) {
          banners = data['item'.hx];
        } else if (key == CommKeyDef.largeCard) {
          isLargeCard = true;
          largeCardData = data['item'.hx][0];
        } else if (key == CommKeyDef.smallCard) {
          isLargeCard = false;
          smallCardData = data['item'.hx][0];
        } else if (key == CommKeyDef.productList) {
          productList = data['item'.hx];
        }
      }
    }
    ReportedDataService().reportData(goToSetting: false);
    if (refresh) {
      update();
    }
  }

  clickApply2(producctData) async {
    if (!App.isLogin()) {
      Get.to(LoginPage(), opaque: false);
      return;
    }

    var isLocationPermis =
        await ReportedDataService().locationPermission(goToSetting: false);

    if (!await ReportedDataService().checkPermission(goToSetting: true)) return;

    if (!isLocationPermis) ReportedDataService().reportData(goToSetting: false);

    BurialPointService.start(BPoint.productAuth, waitLocation: true)
        .then((value) => BurialPointService.end(BPoint.productAuth));
    dynamic data = await HttpRequest().post(
        '/v3/product/apply'.hx, {'product_id'.hx: producctData['id'.hx]},
        loadingText: S.current.loading);
    if (data != null) {
      RouterManage.to(data['url'.hx]);
    }
  }

  // clickApply2(producctData) {
  //   if (!App.isLogin()) {
  //     Get.to(LoginPage(), opaque: false);
  //     return;
  //   }
  //   if (StringUtils.isNotEmpty(indexData?['urlLink'.hx])) {
  //     CommonView.commDialog(
  //         confirm: () async {
  //           Get.back();
  //           if (await Get.to(
  //               MyWebPage(url: indexData?['urlLink'.hx], loanAgree: true))) {
  //             clickApply2(producctData);
  //           }
  //         },
  //         bgImg: Assets.molopNewLoaagrbg,
  //         height: 298.w,
  //         confirmTxt: App.localeEn ? 'Check' : 'Controlar');
  //   } else {
  //     clickApply2(producctData);
  //   }
  // }

  var agree = false;

  // agreeDialog() {
  clickApply(producctData) {
    clickApply2(producctData);
    // if (StringUtils.isNotEmpty(indexData?['urlLink'.hx])) {
    //   agree = false;
    //   var controller = WebViewController()
    //     ..setJavaScriptMode(JavaScriptMode.unrestricted)
    //     ..setBackgroundColor(const Color(0x00000000))
    //     ..setNavigationDelegate(
    //       NavigationDelegate(
    //         onProgress: (int progress) {
    //           // Update loading bar.
    //         },
    //         onPageStarted: (String url) {},
    //         onPageFinished: (String url) {},
    //         onHttpError: (HttpResponseError error) {},
    //         onWebResourceError: (WebResourceError error) {},
    //         onNavigationRequest: (NavigationRequest request) {
    //           if (request.url.startsWith('https://www.www.baidu.com/')) {
    //             return NavigationDecision.prevent;
    //           }
    //           return NavigationDecision.navigate;
    //         },
    //       ),
    //     )
    //     ..loadRequest(Uri.parse(indexData?['urlLink'.hx]));
    //   CommonView.commDialog(
    //       children: [
    //         139.lsh,
    //         LContainer(child: WebViewWidget(controller: controller))
    //             .size(273.w, 320.w)
    //       ],
    //       confirm: () {
    //         Get.back();
    //         clickApply2(producctData);
    //       },
    //       bgImg: myAssets.Assets.molopNewDkxxbg,
    //       height: 546.w,
    //       width: 329.w);
    // } else {
    //   clickApply2(producctData);
    // }
  }
}
